/**
* AutoSWF - Cody Cushing. March 26, 2009
* Copyright (c) 2009 Cody Cushing
* No use restrictions. Maintain this notice in all versions. 
**/

package com.autoswf.core
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import flexunit.framework.TestCase;

	public class HandlerObserverTest extends TestCase
	{
		private var observer:HandlerObserver;
		
		public function HandlerObserverTest(methodName:String=null)
		{
			super(methodName);
		}
		
		override public function setUp():void
		{
			observer = new HandlerObserver(new ChainLink("link"));
		}
		
		/**
		 * Tests that a proxy handler is generated from the supplied handler and returned. 
		 */		
		public function testProxy():void
		{
			var handler:Function = function():void {};
			var func:Function = observer.proxy("functionId", handler);
			assertTrue("Did not return a new proxy function for handler.", handler !== func);
		}
		
		/**
		 * Tests that the same proxy function is returned for an id if that function exists. 
		 */		
		public function testProxySameFunction():void
		{
			var handler:Function = function():void {};
			var func1:Function = observer.proxy("functionId", handler);
			var func2:Function = observer.proxy("functionId", handler);
			assertTrue("Same proxy function was not returned for matching id and handler.", func1 === func2);
		}
		
		/**
		 * Tests that the dynamic function reference is deleted and removed from the dispatcher.
		 */		
		public function testDeleteProxy():void
		{
			var handler:Function = function():void {};
			var func1:Function = observer.proxy("functionId", handler);
			observer.deleteProxy("functionId");
			var func2:Function = observer.proxy("functionId", handler);
			assertTrue("Did not delete the proxy function for the handler.", func1 !== func2);
		}
		
		/**
		 * Tests that the proxy function passes the event and id up and invokes the handler.
		 */		
		public function testProxySplicing():void
		{
			var flag:Boolean = false;
			var handler:Function = function(event:Event):void
			{
				flag = true;
			}
			var dispatcher:EventDispatcher = new EventDispatcher();
			dispatcher.addEventListener(Event.CHANGE, observer.proxy("functionId", handler));
			dispatcher.dispatchEvent(new Event(Event.CHANGE));
			assertTrue("Proxy function did not invoke handler.", flag);
		}
		
		/**
		 * Tests that a proxy function can be destroyed.
		 */		
		public function testRemovingProxySplicing():void
		{
			var flag:Boolean = false;
			var handler:Function = function(event:Event):void // function should not be invoked
			{
				flag = true;
			}
			var dispatcher:EventDispatcher = new EventDispatcher();
			dispatcher.addEventListener(Event.CHANGE, observer.proxy("functionId", handler), false, 0, true);
			dispatcher.removeEventListener(Event.CHANGE, observer.proxy("functionId", handler));
			dispatcher.dispatchEvent(new Event(Event.CHANGE));
			assertFalse("Proxy function still invoked after removal.", flag);		
		}
		
		public function testInvokeHandler():void
		{
			var flag:Boolean = false;
			var handler:Function = function(event:Event):void
			{
				flag = true;
			}

			observer.proxy("functionId", handler);
			observer.sendDownChain(new Transport(new Event(Event.CHANGE),"functionId"));
			assertTrue("Handler not invoked remotely.", flag);
		}
		
	}
}